package com.base.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

public class BaseDaoImpl<T,PK extends Serializable> implements IBaseDao<T,PK>{	
	
	protected Class<T> persistentClass;
	
	public Class<T> getPersistentClass() {
		return persistentClass;
	}	
	
	public void setPersistentClass(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}
	
	
	public BaseDaoImpl(Class<T> persistentClass) {
		this.persistentClass=persistentClass;
	}
	
	public BaseDaoImpl() {
		super();
		this.persistentClass=(Class<T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	private SessionFactory sessionFactory;	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}	
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/**********************基础类操作*********************/
	
	@Override
	public Serializable save(T o) {
		return this.sessionFactory.getCurrentSession().save(o);
	}	
	@Override
	public void delete(T o) {
		this.sessionFactory.getCurrentSession().delete(o);		
	}
	
	@Override
	public void delete(PK id) {
		this.sessionFactory.getCurrentSession().delete(this.get(id));
	}	
	
	@Override
	public void update(T o) {
		this.sessionFactory.getCurrentSession().update(o);
	}
	
	@Override
	public void saveOrUpdate(T o) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(o);		
	}	
	
	/**********************单条记录查询类*********************/	
	
	@Override
	public T get(String hql) {
		Query q=this.sessionFactory.getCurrentSession().createQuery(hql);
		List<T> list=q.list();
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public T get(Class<T> c,Serializable id) {
		return (T) this.sessionFactory.getCurrentSession().get(c, id);		
	}
	
	@Override
	public T get(Serializable id) {
		return (T) this.sessionFactory.getCurrentSession().get(getPersistentClass(), id);		
	}	
	
	@Override
	public T get(String hql, Object[] params) {
		Query q=this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null && params.length>0){
			for(int i=0;i<params.length;i++){
				q.setParameter(i, params[i]);
			}
		}
		List<T> list=q.list();
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public T get(String hql, Map<String, Object> params) {
		Query q=this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null && !params.isEmpty()){
			for(String key:params.keySet()){
				q.setParameter(key, params.get(key));
			}
		}
		List<T> list=q.list();
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	/**********************多条记录查询类*********************/	

	
	@Override
	public List<T> getAll() {
		String queryString="from " + this.getPersistentClass().getName();		
		Query query = this.getSessionFactory().getCurrentSession().createQuery(queryString);  
		List<T> list=query.list();
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}
	
	@Override
	public List<T> getAll(String hql) {
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		List<T> list=query.list();
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}
	
	@Override
	public List<T> getAll(String hql,int page,int rows){
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);		
		List<T> list=query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
		return list;
	}
	
	@Override
	public List<T> getAll(String hql, Map<String, Object> params) {
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null && !params.isEmpty()){
			for(String key:params.keySet()){
				query.setParameter(key, params.get(key));
			}
		}
		List<T> list=query.list();
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}
	
	@Override
	public List<T> getAll(String hql, Map<String, Object> params, int page,int rows) {
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null && !params.isEmpty()){
			for(String key:params.keySet()){
				query.setParameter(key, params.get(key));
			}
		}
		List<T> list=query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}
	
	/**********************数量查询类*********************/	
	
	@Override
	public int count(String hql) {
		Query q=this.sessionFactory.getCurrentSession().createQuery(hql);
		Number num=(Number) q.uniqueResult();
		return num.intValue();
	}
	
	@Override
	public int count(String hql, Map<String, Object> params) {
		Query q=this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params!=null && !params.isEmpty()){
			for(String key:params.keySet()){
				q.setParameter(key, params.get(key));
			}
		}
		Number num=(Number) q.uniqueResult();
		return num.intValue();
	}
	
	
}
